package indi.zhifa.study2024.nbr.monitor.controller;

import indi.zhifa.core.auth.entity.RequireRole;
import indi.zhifa.core.common.web.entity.ann.UnLogin;
import indi.zhifa.core.common.web.entity.ann.ZhiFaRestController;
import indi.zhifa.core.common.web.entity.response.RestResponse;
import indi.zhifa.study2024.common.auth.enums.ERole;
import indi.zhifa.study2024.nbr.monitor.gen.monitor.user.po.GenMonitorRoleEntity;
import indi.zhifa.study2024.nbr.monitor.logic.curd.account.entity.request.*;
import indi.zhifa.study2024.nbr.monitor.logic.curd.account.entity.response.AddUserResponse;
import indi.zhifa.study2024.nbr.monitor.logic.curd.account.entity.response.EditRoleResponse;
import indi.zhifa.study2024.nbr.monitor.logic.curd.account.entity.response.RegisterResponse;
import indi.zhifa.study2024.nbr.monitor.logic.curd.account.service.IUserService;
import indi.zhifa.study2024.nbr.monitor.logic.curd.login.service.ILoginService;
import indi.zhifa.study2024.nbr.monitor.logic.curd.account.service.IRegisterService;
import indi.zhifa.study2024.nbr.monitor.logic.curd.login.entity.request.LoginRequest;
import indi.zhifa.study2024.nbr.monitor.logic.curd.login.entity.response.LoginResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Validated
@RequiredArgsConstructor
@RequestMapping(value = "/api/account")
@ZhiFaRestController
@Tag(name = "账户接口")
public class UserController {
    private final IRegisterService mRegisterService;
    private final IUserService mUserService;
    private final ILoginService mLoginService;

    @UnLogin
    @Operation(summary = "注册")
    @PostMapping("/register")
    public RegisterResponse register(@RequestBody @Validated RegisterRequest registerRequest) {
        return mRegisterService.register(registerRequest);
    }

    @UnLogin
    @Operation(summary = "登录")
    @PostMapping("/login")
    public LoginResponse login(@RequestBody LoginRequest loginRequest) {
        return mLoginService.login(loginRequest);
    }

    @Operation(summary = "修改密码")
    @PostMapping("/changePasswd")
    public RestResponse<String> changePasswd(@RequestBody @Validated ChangePasswdRequest changePasswdRequest) {
        mRegisterService.changePasswd(changePasswdRequest);
        return RestResponse.ok("修改密码成功");
    }

    @RequireRole("sys_account_mgr")
    @Operation(summary = "重置密码")
    @PostMapping("/resetPasswd")
    public RestResponse<String> resetPasswd(@RequestBody @Validated ResetPasswdRequest pResetPasswdRequest) {
        String defaultPasswd = mRegisterService.resetPasswd(pResetPasswdRequest);
        return RestResponse.ok("重置密码成功，默认密码是"+defaultPasswd);
    }

    @RequireRole("sys_account_mgr")
    @Operation(summary = "添加用户")
    @PostMapping("/addUser")
    public AddUserResponse addUser(@RequestBody @Validated AddUserRequest addUserRequest) {
        return mUserService.addUser(addUserRequest);
    }

    @Operation(summary = "获取角色列表")
    @GetMapping("/roles")
    public List<GenMonitorRoleEntity> roles(){
        return mUserService.roleList();
    }

    @RequireRole("ep_boss")
    @Operation(summary = "修改用户角色")
    @PostMapping("/{id}/editRole")
    public EditRoleResponse editRole(@PathVariable(name = "id") Long pId,@RequestBody EditRoleRequest pRequest){
        return mUserService.editRole(pId, pRequest);
    }

}
